﻿//-- =============================================
//-- Author:	    Unknown
//-- Create date:   Unknown
//-- Description:	Bank Factory Class [Get And Rewrite by Saghari, Farid]
//-- E-Mail:    	fredmcs@live.com
//-- Mobile:        0937-241-3499
//-- Last Update:   1390/01/26 23:40    By: Saghari
//-- Important:     سلام دوستان. لطفا کسی این کلاس رو بدون هماهنگی کردن با من تغییر نده
//-- =============================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ChequeManagement.BLL
{
    /// <summary>
    /// This Class contains number of Field, constructor, method and this class inheritance of BaseFactory class
    /// </summary>
    /// <Field>the filed name is "dataObject" this field use in constructor class for get value of DataAccessLayer</Field>
    /// <constructor>this constructor called base constructor and set parameter</constructor>
    /// <method>this class includes some method for add,edit,getall,remove data</method>
    public class BLLBankFactory : BaseFactory
    {

        private ChequeManagement.DAL.DALBank dataObject = null;

        /// <summary>
        /// This constructor called base constructor and set parameter that parameter value is "Bank"
        /// and use on create instance of class
        /// </summary>
        /// <base constructor>base constructor name is "BaseFactory" that get tableName and set tableName with this tableName</base constructor> 
        public BLLBankFactory()
            : base("Bank")
        {
            dataObject = (ChequeManagement.DAL.DALBank)(base.DataAccessLayer);
        }

        /// <summary>
        /// This method get a object that type is ChequeManagement.BLL.BLLBank and check this object is valid or not valid if object is not valid throw a exception
        /// else if object is valid create a instance of ChequeManagement.DAL.DALBank class and call Insert method and set parameter
        /// </summary>
        /// <param name="bankObject" Type="ChequeManagement.BLL.BLLBank"></param>
        /// <return>this type is void and Do not return</return>
        public void Add(ChequeManagement.BLL.BLLBank bankObject)
        {
            try
            {
                if (bankObject.IsValid() == false)
                    throw (new ChequeManagement.Validation.InvalidBusinessObjectException(bankObject.Errors));
                dataObject.Insert(bankObject);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        ///This method get a object that type is ChequeManagement.BLL.BLLBank and a object that type is int
        ///this method create a instance of  ChequeManagement.BLL.BLLBank that name is "bank" and set targetBankID on GetByKey method
        ///if bankObject parameter is IsValid this method call dataObject that type is ChequeManagement.DAL.DALBank and call Update method and set parameter 
        ///else if bankObject parameter not is IsValid throw exception
        /// </summary>
        /// <param name="bankObject" Type="ChequeManagement.BLL.BLLBank"></param>
        /// <param name="targetBankID" Type="int"></param>
        /// <GetByKey method>this method declare on this page and call FetchByKey method on DAL layer and set parameter</GetByKey method>
        /// <return>this type is void and Do not return</return>
        public void Edit(ChequeManagement.BLL.BLLBank bankObject, int targetBankID)
        {
            try
            {
                ChequeManagement.BLL.BLLBank bank = this.GetByKey(targetBankID);

                if ((bank != null) && (bank.BankName.ToLower() == bankObject.BankName.ToLower()))
                {
                    if (bankObject.IsValid("BankName") == false)
                        throw (new ChequeManagement.Validation.InvalidBusinessObjectException(bankObject.Errors));
                }
                else
                {
                    if (bankObject.IsValid() == false)
                        throw (new ChequeManagement.Validation.InvalidBusinessObjectException(bankObject.Errors));
                }
                dataObject.Update(bankObject, targetBankID);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have a parameter that type is DataTable
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchAll method and set parameter for select all data 
        /// </summary>
        /// <param name="dataTable" Type="DataTable"></param>
        /// <return>this type is void and Do not return but parameter is DataTable and result return into this DataTable</return>
        public void GetAll(DataTable dataTable)
        {
            try
            {
                dataObject.FetchAll(dataTable);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method not have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchAll method for select all data
        /// </summary>
        /// <returns>this method select all data and result return into List<> that type is BLL.BLLBank</returns>
        public List<BLL.BLLBank> GetAll()
        {
            try
            {
                return (dataObject.FetchAll());
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameters
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchByField method and set parameters
        /// the filedname parameter select field name on select table and vlaue parameter select value that select field name and result return into dataTable parameter 
        /// </summary>
        /// <param name="fieldName" Type="ChequeManagement.BLL.BLLBank.BankField"></param>
        /// <param name="value" Type="object"></param>
        /// <param name="dataTable" Type="ref DataTable"></param>
        /// <return>this type is void and Do not return but parameter is DataTable and result return into this DataTable</return>
        public void GetByField(ChequeManagement.BLL.BLLBank.BankField fieldName, object value, ref DataTable dataTable)
        {
            try
            {
                dataObject.FetchByField(fieldName.ToString(), value, dataTable);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchByField method and set parameters
        /// the filedname parameter select field name on select table and vlaue parameter select value that select field name
        /// </summary>
        /// <param name="fieldName" Type="ChequeManagement.BLL.BLLBank.BankField"></param>
        /// <param name="value" Type="object"></param>
        /// <returns>this method select data and result return into List<> that type is BLL.BLLBank</returns>
        public List<BLL.BLLBank> GetByField(ChequeManagement.BLL.BLLBank.BankField fieldName, object value)
        {
            try
            {
                return (dataObject.FetchByField(fieldName.ToString(), value));
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchAllByCondition method and set parameters
        /// select data by this condition 
        /// </summary>
        /// <param name="condition" Type="string"></param>
        /// <param name="dataTable" Type="ref DataTable"></param>
        /// <return>this type is void and Do not return but parameter is DataTable and result return into this DataTable</return>
        public void GetAllByCondition(string condition, ref DataTable dataTable)
        {
            try
            {
                dataObject.FetchAllByCondition(condition, dataTable);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call FetchByKey method and set parameters
        /// select data by targetBankID
        /// </summary>
        /// <param name="targetBankID" Type="int"></param>
        /// <returns>object that type is BLL.BLLBank </returns>
        public BLL.BLLBank GetByKey(int targetBankID)
        {
            try
            {
                return (dataObject.FetchByKey(targetBankID));
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call Delete method and set parameters
        /// </summary>
        /// <param name="targetBankID" Type="int"></param>
        /// <return>this type is void and Do not return</return>
        public void Remove(int targetBankID)
        {
            try
            {
                dataObject.Delete(targetBankID);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        /// <summary>
        /// This method have parameter
        /// and call dataObject that type is ChequeManagement.DAL.DALBank and call Delete method and set parameters
        /// remove field from table by this fieldName that value is this value
        /// </summary>
        /// <param name="fieldName" Type="ChequeManagement.BLL.BLLBank.BankField"></param>
        /// <param name="value" Type="object"></param>
        /// <return>this type is void and Do not return</return>
        public void Remove(ChequeManagement.BLL.BLLBank.BankField fieldName, object value)
        {
            try
            {
                dataObject.Delete(fieldName.ToString(), value);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
    }
}
